package IntentMessage;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.example.myapplication.ArrayAdapterInclude.IntentArrayInfoAdapter;
import com.example.myapplication.ArrayAdapterInclude.IntentArrayListAdapter;
import com.example.myapplication.Person;
import com.example.myapplication.R;
import java.util.ArrayList;

public class IntentSubInfoActivity extends AppCompatActivity {
  String[][]infoString;
  ArrayList<Person> personArrayList;
  Person person;
  Bitmap bitmap;
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.intent_test_sublayout3);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("数据传递测试");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    SelectData();


  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }

  private void GoBack() {
    finish();
  }

  private void SelectData() {
    GetArrayData();
    GetListArrayData();
    GetObjectData();
    GetBitmap();
    if (infoString != null) {
      LinkedViewByArray();
    } else if (personArrayList != null) {
      LinkedViewByArrayList();
    } else if (person!=null) {
      LinkedViewByObject();
    } else if (bitmap!=null) {
      ShowImage();
    }
  }

  private void GetArrayData() {
    infoString = (String[][]) getIntent().getExtras().getSerializable("InfoArray");

  }
  private void GetListArrayData() {
    personArrayList = (ArrayList<Person>) getIntent().getExtras().getSerializable("InfoList");

  }
  private void GetObjectData() {
    person = (Person) getIntent().getExtras().getSerializable("singlePerson");


  }
  private void GetBitmap() {
    bitmap = (Bitmap) getIntent().getExtras().getParcelable("bitmap");
  }
  private void LinkedViewByArray() {
    getSupportActionBar().setTitle("传递Array数据");
    IntentArrayInfoAdapter adapter = new IntentArrayInfoAdapter(this, infoString);
    ListView listView = findViewById(R.id.infolist);
    listView.setAdapter(adapter);
  }
  private void LinkedViewByArrayList() {
    getSupportActionBar().setTitle("传递ArrayList集合数据");
    IntentArrayListAdapter adapter = new IntentArrayListAdapter(this, personArrayList);
    ListView listView = findViewById(R.id.infolist);
    listView.setAdapter(adapter);
  }
  private void LinkedViewByObject() {
    getSupportActionBar().setTitle("传递Object数据");
    ArrayList<Person> data = new ArrayList<>();
    data.add(person);
    IntentArrayListAdapter adapter = new IntentArrayListAdapter(this, data);
    ListView listView = (ListView) findViewById(R.id.infolist);
    listView.setAdapter(adapter);

  }
  private void ShowImage() {
    getSupportActionBar().setTitle("传递bitmap");
    ImageView imageView = (ImageView) findViewById(R.id.ImgView);
    imageView.setImageBitmap(bitmap);
  }
}
